layui.define(['table', 'form', 'laypage', 'laytpl', 'layer', 'laydate'], function (exp) {
    var table = layui.table
        , form = layui.form
        , $ = layui.$
        , laydate = layui.laydate
        , layer = layui.layer
        , laytpl = layui.laytpl;

    // 渲染数据表格
    let typeTable = table.render({
        id: 'typeTable',
        elem: '#type-table',
        url: '/type/page',
        method: 'get',
        toolbar: "#type-toolbar",
        defaultToolbar: ['filter', 'print'],
        request: {
            pageName: 'page' // 页码的参数名称，默认：page
            , limitName: 'size' // 每页数据量的参数名，默认：limit
        },
        parseData: function (res) {
            return {
                "data": res.payload.records,
                'code': res.code,
                'msg': res.msg,
                "count": res.payload.total
            }
        },
        response: {
            statusCode: 200
        },
        loading: true,
        cols: [[
            {type: 'checkbox', fixed: 'left'},
            {type: 'numbers', field: 'id', sort: true, title: '序号', align: 'center', width: '5%'},
            {field: 'name', align: 'center', title: '类型名称'},
            {title: '操作', align: 'center', fixed: 'right', toolbar: '#type-bar', width: 320}
        ]],
        page: true,
        limits: [2, 4, 6, 8, 10, 20]
        , text: {
            none: '还没有类型!'
        }, done: function (res, curr, count) {
            this.where = {};
        }
    });

    // 监听列工具条
    table.on('tool(type-table)', function (obj) {
        const data = obj.data,
            event = obj.event,
            tr = obj.tr;
        if (event === 'del') {
            const loadIndex = layer.load()
            $.post('/type/delete/' + data.id, function (result) {
                layer.close(loadIndex)
                if (result.success) {
                    obj.del()
                }
            })
        }
    });

    // 监听左上工具条
    table.on('toolbar(type-table)', function(obj){
        var checkStatus = table.checkStatus(obj.config.id);
        switch(obj.event){
            case 'delete':
                if (checkStatus.data.length === 0) {
                    layer.msg('请选择!', {icon: 5});
                    return;
                }
                layer.confirm('确定批量删除他们吗?', {icon: 3, title:'确认'}, function(index) {
                    var data = checkStatus.data
                        , ids = []
                        , loadIndex;
                    loadIndex = layer.load()
                    $.each(data, function (index, value) {
                        ids.push(value.id)
                    });

                    $.post('/type/delete/more', {"ids": ids}, function (result) {
                        layer.close(loadIndex)
                        if (result.success) {
                            typeTable.reload({
                                where: {
                                    page: 1,
                                    size: 10
                                }
                            })
                        }
                    })
                    layer.close(index);
                });
                break;
            case 'add':
                layer.prompt({
                    title: '类型标题',
                }, function(value, index, elem){
                    layer.close(index);
                    const loadIndex = layer.load()
                    $.post('/type/add', {"name": value}, function (result) {
                        layer.close(loadIndex)
                        if (result.success) {
                            typeTable.reload({
                                where: {
                                    page: 1,
                                    size: 10
                                }
                            })
                        }
                    })
                });
                break;
        }
    });

    // 查询工具
    form.on('submit(search-type)', function (data) {
        table.reload('typeTable', {
            where: {
                name: data.field['name'],
            }
        });
        form.render();
    });

    // 重置查询
    form.on('submit(type-search)', function (data) {
        form.val("type-form", {
            "name": ""
        });
        typeTable.reload({
            where: {
                name: null,
            }
        });
        form.render();
    })

    exp('type', {});
});